package com.sxxm.qxgl.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxxm.qxgl.entity.SysDept;
import com.sxxm.qxgl.entity.SysMenu;
import com.sxxm.qxgl.mapper.SysMenuMapper;
import com.sxxm.qxgl.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxxm.qxgl.vo.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2021-09-14
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    SysMenuMapper sysMenuMapper;
    public SysMenuServiceImpl(SysMenuMapper sysMenuMapper){
        this.sysMenuMapper=sysMenuMapper;
    }
    @Override
    public Result cx(SysMenu sysMenu) {
        // 从数据库获取全部数据
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("name", sysMenu.getName());
        if (sysMenu.getId() != null) {
            queryWrapper.eq("id", sysMenu.getId());
            List<SysDept> menus = sysMenuMapper.selectList(queryWrapper);
            return Result.ok().message("成功").data(menus);
        }
        if (sysMenu.getStatus().equals("0")) {
            queryWrapper.eq("status", sysMenu.getStatus());
        }
        if (sysMenu.getStatus().equals("1")) {
            queryWrapper.eq("status", sysMenu.getStatus());
        }
        List<SysMenu> menus = sysMenuMapper.selectList(queryWrapper);

        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",
                (treeNode, tree) -> {
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSortCode());
                    tree.setName(treeNode.getName());
                    tree.putExtra("status",treeNode.getStatus());
                    tree.putExtra("imgUrl",treeNode.getImgUrl());
                    tree.putExtra("appUrl",treeNode.getAppUrl());
                    tree.putExtra("sortCode",treeNode.getSortCode());
                });

        return Result.ok().message("成功").data(treeNodes);
    }

    @Override
    public Result ModifyThe(SysMenu sysUser) {
        QueryWrapper<SysMenu> Wrapper = new QueryWrapper<>();
        Wrapper.lambda().eq(SysMenu::getId, sysUser.getId());
        //修改条件
        SysMenu user = sysMenuMapper.selectOne(Wrapper);
        sysUser.setId(user.getId());
        int a = sysMenuMapper.updateById(sysUser);
        if (a <= 0) {
            return Result.error().message("修改失败");
        }
        return Result.ok().message("修改成功");
    }

    @Override
    public Result Addition(SysMenu sysUser) {
        if (sysMenuMapper.insert(sysUser) <= 0) {
            return Result.error().message("添加失败");
        }
        return Result.ok().message("添加成功");
    }

    @Override
    public Result Delete(long id) {
        if (sysMenuMapper.deleteById(id) <= 0) {
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    @Override
    public Result zt(long id) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMenu::getId, id);
        //修改条件
        SysMenu user = sysMenuMapper.selectOne(queryWrapper);
        SysMenu user1 = new SysMenu();
        user1.setId(user.getId());
        if (sysMenuMapper.selectById(id).getStatus().equals("0")) {
            user1.setStatus("1");
        }else {
            user1.setStatus("0");
        }
        if (sysMenuMapper.updateById(user1) <= 0) {
            return Result.error().message("状态修改失败");
        }
        return Result.ok().message(blzt(user.getId(),user1));
    }

    @Override
    public Result dt(long id) {
        return null;
    }

    public String blzt(Long id,SysMenu user1){
        QueryWrapper<SysMenu> query = new QueryWrapper<>();
        query.eq("parentId",id);
        List<SysMenu> a = sysMenuMapper.selectList(query);
        for (int i =0;  i<a.size();i++){
            System.out.println(a.get(i).getId());
            user1.setId(a.get(i).getId());
            if (sysMenuMapper.updateById(user1) <= 0) {
                return "状态修改失败";
            }
            blzt(a.get(i).getId(),user1);
        }
        return "状态修改成功";
    }
}
